package com.sk.coin.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sk.coin.utils.ContextUtil;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @author: SongKun
 * @date: 2022/3/2
 * @Description:
 */
public class AutoFillHandler implements MetaObjectHandler {

    /**
     * 插入时自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = ContextUtil.getUserId();
        // 严格的字段填充，能匹配，字段上有注解并且值不为空时才进行填充
        this.strictInsertFill(metaObject,"created",Date.class,new Date());
        this.strictInsertFill(metaObject,"createBy",Long.class,userId);
        this.strictInsertFill(metaObject,"lastUpdateTime", Date.class,new Date());
    }

    /**
     * 更新时自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = ContextUtil.getUserId();
        this.strictUpdateFill(metaObject,"lastUpdateTime",Date.class,new Date());
        this.strictUpdateFill(metaObject,"modifyBy",Long.class, userId);
    }
}
